<template>
	<view>		
		<u-popup :show="userLoginShow" @close="userLoginClose" mode="center" :safeAreaInsetBottom="false" round="10">
			<view class="wrap cflex jcc radius10 f16">
				<text class="w100percent taligncenter">请先登录</text>
				<view class="mt12">
					<u-line></u-line>
				</view>	
				<text class="mt12">微信一键登录后即可使用完整功能</text>
				<view class="login_button dflex mt20">
					<button type="default" style="width: 50%; font-size: 14px;" @click="userLoginClose">取消</button>
					<button type="warning" class="ml12 main_bg" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber" style="width: 50%; font-size: 14px;">确认登录</button>
				</view>
			</view>
		</u-popup>	
	</view>
</template>

<script>
	// 全局登录判断
	// 判断是否登录,已登录返回already,未登录弹起登录提示框
	// 没有登录,拉起微信手机号一键验证,然后把返回数据到父组件继续后续操作
	// 记得在父组件调用 getWxCode 获取code不然登录错误 
	
	
	export default {
		name:"userCheck",
		data() {
			return {
				userLoginShow : false,
				code : ''
			};
		},
		methods: {			
			userLoginClose() {
				this.userLoginShow = false
			},
			user_check() {
				uni.showLoading({
					title: "加载中...."
				})
				uni.request({
					url: uni.getStorageSync('domain') + '/api/v1/user',
					method: 'get',
					header: {
						'Accept': 'application/json',
						'Authorization': 'Bearer ' + uni.getStorageSync('access_member_info').access_token
					},
					success: (res) => {
						if(res.statusCode == 401) {
							//弹出登录提示框
							this.userLoginShow = true
							uni.hideLoading();
						} else {
							this.$emit("userCheckStatus", 'already')
							// this.getHagglecode()
							// console.log('success',res)
						}
					}
				})
			},
			getWxCode() {
				uni.login({
					provider:'weixin',
					success: (res) => {
						this.code = res.code
						// console.log('this.code', this.code)
					}
				})
			},
			decryptPhoneNumber(e) {
				this.userLoginShow = false
				uni.showLoading({
					title: "加载中...."
				})
				this.errMsg = e.detail.errMsg
				this.iv = e.detail.iv
				this.encryptedData  = e.detail.encryptedData
				//弹出登录提示框
				//用户取消，这个值就获取不到，就不要发请求到服务器了
				if(e.detail.encryptedData && this.code != '') {
					uni.request({
						url: uni.getStorageSync('domain') + '/api/v1/user/miniphonenumber',
						method: 'POST',
						data: {
							errMsg:this.errMsg,
							iv: this.iv,
							encryptedData : this.encryptedData,
							code : this.code
						},
						success: (res) => {
							// console.log('this.code', this.code)
							if (res.statusCode != 201) {
								console.log(res.data.message);
								uni.hideLoading();
								// this.show_toast()
								// uni.$u.toast(res.data.message);
							} else {
								this.$emit("userCheckStatus", res)
								// uni.setStorageSync("access_member_info", res.data)
								// this.getHagglecode()
								// console.log('userinfo', uni.getStorageSync("access_member_info"))
							}
						},
						fail: (res) => {}
					})
				} else {
					uni.hideLoading()
				}
			},
		}
	}
</script>

<style>

</style>